import {
  api, util, dialog, clickIncrement,
  clearIncrement } from '../../../js/util'
import { orderlist, grouporder } from "../../../js/modules/order.js"
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  behaviors: [componentBehavior],
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    cancelItems: [{ value: '现在不想购买', checked: 'checked' },
    { value: '支付不成功' },
    { value: '商品价格较高' },
    { value: '等待时间过长' },
    { value: '卖家缺货' },
    { value: '更换或者添加新商品' },
    { value: '错误或重复下单' },
    { value: '其他' }
    ],
    cancelValue:"现在不想购买",
    isSubmit:false
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /**
     * 展开遮罩层
     */
    showModal: function (e, id,order_desc) {
      this.data.orderId = id;
      this.data.order_desc = order_desc||"";
      dialog.showModal(this, 'showModalStatus')
    },
    /**
     * 隐藏遮罩层
     */
    hideModal: function () {
      dialog.hideModal(this, 'showModalStatus')
    },
    /**
     * 选择取消原因 
     * */
    switchCancelItem: function (e) {
      var cancelReasonStyle = "none";
      if (e.detail.value == "其他") {
        cancelReasonStyle = "block";
      } else {
        this.setData({
          cancelReason: ""
        });
      }
      this.setData({
        cancelValue: e.detail.value
      });
    },
    /**
     * 输入取消订单原因
     *  */
    cancelReasonInput: function (e) {
      this.setData({
        cancelReason: e.detail.value
      });
    },
    /**
     * 选择取消原因
     *  */
    onCancelOrderEvent: function (e) {
      var that = this;
      var id = this.data.orderId;
      let { cancelValue, cancelReason, order_desc, isSubmit } = this.data;
      if (isSubmit) {
        return;
      }
      if (cancelValue == "其他" && (cancelReason == undefined || cancelReason == "")) {
        util.alert("请填写取消订单的原因");
        return;
      }
      clickIncrement(function () {
        that.setData({
          isSubmit: true
        }, () => {

          if (order_desc == "") {

            var data = {
              OrderId: id,
              Cause: cancelValue != "其他" ? cancelValue : "其它原因",
              OtherCause: cancelValue == "其他" ? cancelReason : ""
            }
            orderlist.cancelOrder(data, () => {
              that.setData({
                cancelValue: "现在不想购买",
                isSubmit: false
              })
              that.triggerEvent("onCancelOrderEvent");
              dialog.hideModal(that, 'showModalStatus')

            },()=>{
              that.setData({
                isSubmit: false
              })
            });
          }
          else {
            var data = {
              Id: id,
              Cause: "买家取消" + order_desc
            }
            grouporder.cancelOrder(data, () => {
              that.setData({
                isSubmit: false
              })
              that.triggerEvent("onCancelOrderEvent");
              dialog.hideModal(that, 'showModalStatus')
            }, () => {
              that.setData({
                isSubmit: false
              })
            });
          }
        })
      })

    },
  }
})
